package dao;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

import util.dataUtil;
import model.PostMotemModel;

public class PostMotemDao {
	public static ArrayList getALL(){
		ArrayList listPostMotem = new ArrayList();
		
			try {
				String sql = "SELECT * FROM PostMotem";
				PreparedStatement ps = dataUtil.getconnConnection().prepareStatement(sql);
				ResultSet rs = ps.executeQuery();
				while (rs.next()){
					PostMotemModel model = new PostMotemModel();
					model.setAddress(rs.getString("ADDRESS"));
					model.setAge(rs.getInt("AGE"));
					model.setDate(rs.getString("DATE"));
					model.setId(rs.getInt("ID"));
					model.setDoctor(rs.getString("DOCTOR"));
					model.setImage(rs.getString("IMAGE"));
					model.setResult(rs.getString("RESULT"));
					model.setIdHis(rs.getInt("IDHIS"));
					model.setLastUpdated(rs.getString("Last_updated"));
					model.setName(rs.getString("NAME"));
					model.setPlace(rs.getString("PLACE"));
					model.setSex(rs.getInt("SEX"));
					listPostMotem.add(model);
				}
					
			} catch (Exception e) {
				e.printStackTrace();
			}
			return listPostMotem;
	}
	public static ArrayList<PostMotemModel> getbyID(int id){
		ArrayList<PostMotemModel> listpos = new ArrayList<PostMotemModel>();
		PostMotemModel model = null;
		String sql = "select * from POSTMOTEM where ID =?";
		try {
			PreparedStatement ps = dataUtil.getconnConnection()
					.prepareStatement(sql);
			ps.setInt(1, id);
			ResultSet rs = ps.executeQuery();
			while(rs.next()){
				model = new PostMotemModel();
				model.setAddress(rs.getString("ADDRESS"));
				model.setAge(rs.getInt("AGE"));
				model.setDate(rs.getString("DATE"));
				model.setId(rs.getInt("ID"));
				model.setDoctor(rs.getString("DOCTOR"));
				model.setImage(rs.getString("IMAGE"));
				model.setResult(rs.getString("RESULT"));
				model.setIdHis(rs.getInt("IDHIS"));
				model.setLastUpdated(rs.getString("LAST_UPDATED"));
				model.setName(rs.getString("NAME"));
				model.setPlace(rs.getString("PLACE"));
				model.setSex(rs.getInt("SEX"));
				listpos.add(model);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return listpos;
		
	}
	//search posmortem
	public static ArrayList<PostMotemModel> getdataSearchPostmortem(String name){
		ArrayList<PostMotemModel> listpos = new ArrayList<PostMotemModel>();
		PostMotemModel model = null;
		String sql = "select * from POSTMOTEM where name like '%"+name+"%'";
		try {
			PreparedStatement ps = dataUtil.getconnConnection()
					.prepareStatement(sql);
			ResultSet rs = ps.executeQuery();
			while(rs.next()){
				model = new PostMotemModel();
				model.setAddress(rs.getString("ADDRESS"));
				model.setAge(rs.getInt("AGE"));
				model.setDate(rs.getString("DATE"));
				model.setId(rs.getInt("ID"));
				model.setDoctor(rs.getString("DOCTOR"));
				model.setImage(rs.getString("IMAGE"));
				model.setResult(rs.getString("RESULT"));
				model.setIdHis(rs.getInt("IDHIS"));
				model.setLastUpdated(rs.getString("LAST_UPDATED"));
				model.setName(rs.getString("NAME"));
				model.setPlace(rs.getString("PLACE"));
				model.setSex(rs.getInt("SEX"));
				listpos.add(model);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return listpos;
		
	}
	
	public static int insertPostmotem(PostMotemModel model){
		int kq =0;
		try {
			CallableStatement csmt = dataUtil.getconnConnection().prepareCall("{call sp_InsertPOSTMOTEM(?,?,?,?,?,?,?,?,?,?,?,?)}");
			csmt.setInt("IDHIS", model.getIdHis());
			csmt.setString("NAME", model.getName());
			csmt.setInt("AGE", model.getAge());
			csmt.setInt("SEX", model.getSex());
			csmt.setString("IMAGE", model.getImage());
			csmt.setString("ADDRESS", model.getAddress());
			csmt.setString("DOCTOR", model.getDoctor());
			csmt.setString("RESULT", model.getResult());
			csmt.setString("PLACE", model.getPlace());
			csmt.setString("DATE", model.getDate());
			csmt.setString("Last_updated", model.getLastUpdated());
			csmt.registerOutParameter("rs", java.sql.Types.INTEGER);
			csmt.execute();
			kq = csmt.getInt("rs");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return kq;
	}
	public static int updatePostmotem(PostMotemModel model){
		int kq =0;
		try {
			CallableStatement csmt = dataUtil.getconnConnection().prepareCall("{call sp_UpdatePOSTMOTEM(?,?,?,?,?,?,?,?,?,?,?,?,?)}");
			csmt.setInt("ID", model.getId());
			csmt.setInt("IDHIS", model.getIdHis());
			csmt.setString("NAME", model.getName());
			csmt.setInt("AGE", model.getAge());
			csmt.setString("IMAGE", model.getImage());
			csmt.setInt("SEX", model.getSex());
			csmt.setString("ADDRESS", model.getAddress());
			csmt.setString("DOCTOR", model.getDoctor());
			csmt.setString("RESULT", model.getResult());
			csmt.setString("PLACE", model.getPlace());
			csmt.setString("DATE", model.getDate());
			csmt.setString("Last_updated", model.getLastUpdated());
			csmt.registerOutParameter("rs", java.sql.Types.INTEGER);
			csmt.execute();
			kq = csmt.getInt("rs");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return kq;
	}
	public static int deletePostMotem(PostMotemModel model){
		int kq =0;
		try {
			CallableStatement csmt = dataUtil.getconnConnection().prepareCall("{call sp_DeletePOSTMOTEM(?,?)}");
			csmt.setInt("ID", model.getId());
			csmt.registerOutParameter("result", java.sql.Types.INTEGER);
			csmt.execute();
			kq = csmt.getInt("result");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return kq;
	}
}
